/*******************************************************************************
*   				               DOFILE: GAME GRAPHS					                       *
********************************************************************************

    **  OUTLINE:

        - All game graphs

********************************************************************************/

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

  * Load dataset

  use "$Endline_dtFin/data_angolacbm.dta", clear

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

global sXb teachersbaseline classroomsbaseline studentsbaseline

drop if t!=1

foreach var in parentmob1 parentmob2 parentsonM parentonT P_perf_student Gp_dgf0_2 Gp_tg{
  replace `var' = . if regparent!=1
}

foreach var in parentmobT pres_teacher Gt_dg_r1 Gt_tg{
  replace `var' = . if regteacher!=1
}

foreach var in formalparti onF publicinfo safety higiene material directoronT A_repeat_share{
  replace `var' = . if regschool!=1
}

foreach var in parentmob1 parentmob2 parentmobT formalparti parentsonM onF publicinfo safety higiene material pres_teacher parentonT directoronT Gt_dg_r1 Gt_tg P_perf_student A_repeat_share pres_stud1 test_34 Gp_dgf0_2 Gp_tg{
  bysort school_id: egen `var'_mean = mean(`var')
  replace `var'_mean = . if regschool!=1
}

sum parentmob1_mean parentmob2_mean parentmobT_mean formalparti_mean parentsonM_mean onF_mean publicinfo_mean safety_mean higiene_mean material_mean pres_teacher_mean parentonT_mean directoronT_mean Gt_dg_r1_mean Gt_tg_mean P_perf_student_mean A_repeat_share_mean pres_stud1_mean test_34_mean Gp_dgf0_2_mean Gp_tg_mean

foreach var in parentmob1_mean parentmob2_mean parentmobT_mean formalparti_mean parentsonM_mean onF_mean publicinfo_mean safety_mean higiene_mean material_mean pres_teacher_mean parentonT_mean directoronT_mean Gt_dg_r1_mean Gt_tg_mean P_perf_student_mean A_repeat_share_mean pres_stud1_mean test_34_mean Gp_dgf0_2_mean Gp_tg_mean{
  sum `var'
  gen `var'_z = (`var'-r(mean))/r(sd)
}

replace A_repeat_share_mean_z = A_repeat_share_mean_z*-1

egen index1 = rowmean(parentmob1_mean_z)
egen index2 = rowmean(parentmob2_mean_z parentmobT_mean_z formalparti_mean_z)
*egen index2 = rowmean(parentmob1_mean_z parentmob2_mean_z parentmobT_mean_z formalparti_mean_z Gp_dgf0_2_mean_z Gp_tg_mean_z)
egen index3 = rowmean(parentsonM_mean_z onF_mean_z publicinfo_mean_z safety_mean_z higiene_mean_z material_mean_z)
egen index4 = rowmean(pres_teacher_mean_z parentonT_mean_z directoronT_mean_z)
*egen index4 = rowmean(pres_teacher_mean_z parentonT_mean_z directoronT_mean_z Gt_dg_r1_mean_z Gt_tg_mean_z)
egen index5 = rowmean(P_perf_student_mean_z A_repeat_share_mean_z pres_stud1_mean_z test_34_mean_z)

foreach var in index1 index2 index3 index4 index5{
  sum `var' if T0==1
  gen `var'_z = (`var'-r(mean))/r(sd)
}

// Graph 6: Summary

cap mat drop results

	* run regressions

  foreach var in index1_z index2_z index3_z index4_z index5_z{
    qui: reg `var' T1 T2 T3 i.V $sXb if t==1 & regschool==1, robust
      mat a = r(table)
      mat b = a[1,1..3] \ a[5..6,1..3] \ a[2,1..3]
      mat results = nullmat(results) , b
  }

	* Set up matrix for graphing

		mat results = results'

		clear
		svmat results

		gen lower90 = results1 - (1.645*results4)
		gen upper90 = results1 + (1.645*results4)

		gen treat = mod(_n,3)
		  replace treat = 3 if treat == 0

		gen var = .
		local var = 1
		qui count
		  forv i = 1/`r(N)' {
		    replace var = `var' in `i'
		    if mod(`i',3) == 0 local ++var
		  }

		sort var treat
		gen place = _n

		replace place = place + (var-1)*2

		forv i = 1/3{
			gen T`i' = results1 if treat==`i'
		}

		su place
			local max = `r(max)' +1

	* Create Graph6

		tw ///
		  (rspike results2 results3 place, lw(vthin) lc(gs0) hor) ///
			(rcap lower90 upper90 place, lw(vthin) lc(gs0) hor) ///
		  (scatter place T1,  msymbol(O) mcolor(gs0)) ///
		  (scatter place T2,  msymbol(D) mcolor(gs0)) ///
		  (scatter place T3,  msymbol(S) mcolor(gs0)) ///
		  ,	/// twoway_options :
		  scheme(s1mono) ///
		  ysc(reverse r(0 `max')) ///
      xsc(r(-1.5 1.5)) ///
			xlab(-1.5(0.5)1.5) ///
		  ylab(2 "Parents' mobilization at home" 7 "Parents' mobilization at school" ///
       12 "Quality of school management" 17 "Teachers' performance" 22 "Students' performance", ///
		  	angle(0) labsize(small)) ///
		  ytitle("") ///
      xline(0) ///
		  leg(order(3 4 5) lab(3 "T1: Information") lab(4 "T2: Meetings") ///
		  lab(5 "T3: Combined") cols(3) si(3))	///
		  ti("", span)	///
		  sub("Treatment effects grouped by type of effect")	///
		  note( "Note:  Coefficient points in standard deviations. Lines indicate the 95 "  ///
		  	"percent confidence intervals, with a cap at 90 percent.")	///
		  saving("$Endline_outRaw/Figure6.gph", replace)

graph export "$Endline_outFin/Figure6.png", as(png) name("Graph") replace
// end of dofile







*
